﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Rally_like_a_madman.Data;
using System.Collections.ObjectModel;
using System.Windows.Navigation;

namespace Rally_like_a_madman
{
    public partial class CreateRoute : PhoneApplicationPage
    {
        private ObservableCollection<RouteWaypointLink> routeWaypointsLinkList;
        private Route activeRoute;

        /// <summary>
        /// 
        /// </summary>
        public CreateRoute()
        {
            InitializeComponent();

            routeWaypointsLinkList = new ObservableCollection<RouteWaypointLink>();

            lbRouteWaypointLinks.ItemsSource = routeWaypointsLinkList;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbName_GotFocus(object sender, RoutedEventArgs e)
        {
            if (tbName.Text == "Naam van de Route")
                tbName.Text = string.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbName_LostFocus(object sender, RoutedEventArgs e)
        {
            if (tbName.Text == string.Empty)
                tbName.Text = "Naam van de Route";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMoveWaypointUp_Click(object sender, RoutedEventArgs e)
        {
            int selectedIndex = lbRouteWaypointLinks.SelectedIndex;
            if (selectedIndex >= 0)
            {
                RouteWaypointLink selectedRouteWaypointLink = routeWaypointsLinkList[selectedIndex];
                if (selectedRouteWaypointLink != null && selectedIndex > 0)
                {
                    //Change the positions around
                    routeWaypointsLinkList.Insert(selectedIndex - 1, selectedRouteWaypointLink);
                    routeWaypointsLinkList.RemoveAt(selectedIndex + 1);
                    lbRouteWaypointLinks.SelectedIndex = selectedIndex - 1;

                    //Update the WaypointIndex of the 2 moved items
                    routeWaypointsLinkList[selectedIndex - 1].WaypointIndex--;
                    routeWaypointsLinkList[selectedIndex].WaypointIndex++;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMoveWaypointDown_Click(object sender, RoutedEventArgs e)
        {
            int selectedIndex = lbRouteWaypointLinks.SelectedIndex;
            if (selectedIndex >= 0)
            {
                RouteWaypointLink selectedRouteWaypointLink = routeWaypointsLinkList[selectedIndex];
                if (selectedRouteWaypointLink != null && selectedIndex < routeWaypointsLinkList.Count - 1)
                {
                    //Change the positions around
                    routeWaypointsLinkList.Insert(selectedIndex + 2, selectedRouteWaypointLink);
                    routeWaypointsLinkList.RemoveAt(selectedIndex);
                    lbRouteWaypointLinks.SelectedIndex = selectedIndex + 1;

                    //Update the WaypointIndex of the 2 moved items
                    routeWaypointsLinkList[selectedIndex + 1].WaypointIndex++;
                    routeWaypointsLinkList[selectedIndex].WaypointIndex--;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRemoveWaypoint_Click(object sender, RoutedEventArgs e)
        {
            int selectedIndex = lbRouteWaypointLinks.SelectedIndex;
            if (selectedIndex >= 0)
            {
                RouteWaypointLink selectedRouteWaypointLink = routeWaypointsLinkList[selectedIndex];
                if (selectedRouteWaypointLink != null)
                {
                    routeWaypointsLinkList.RemoveAt(selectedIndex);

                    //Update the WaypointIndex of every item after the removed item
                    for (int i = selectedIndex; i < routeWaypointsLinkList.Count; i++)
                        routeWaypointsLinkList[i].WaypointIndex--;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            AddExistingWaypointPage destionationExistingWaypoint = e.Content as AddExistingWaypointPage;
            if (destionationExistingWaypoint != null)
            {
                destionationExistingWaypoint.routeWaypointLinks = routeWaypointsLinkList;
                destionationExistingWaypoint.activeRoute = this.activeRoute;
            }
            else
            {
                AddNewWaypointPage destionationNewWaypoint = e.Content as AddNewWaypointPage;
                if (destionationNewWaypoint != null)
                {
                    destionationNewWaypoint.routeWaypointLinks = routeWaypointsLinkList;
                    destionationNewWaypoint.activeRoute = this.activeRoute;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExistingWaypoint_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/AddExistingWaypointPage.xaml", UriKind.RelativeOrAbsolute));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddRoute_Click(object sender, RoutedEventArgs e)
        {
            if (tbName.Text != string.Empty && tbName.Text != "Naam van de Route" && lbRouteWaypointLinks.Items.Count > 0)
            {
                if (NavigationHelper.CheckIfRouteExists(tbName.Text))
                    MessageBox.Show("Een Route met deze naam bestaat al. Geef deze Route een andere naam om verder te gaan.");
                else
                {
                    activeRoute.Name = tbName.Text;

                    //Add activeRoute to RouteTable
                    App thisApp = Application.Current as App;
                    thisApp.ActiveDB.RouteTable.InsertOnSubmit(activeRoute);

                    //Add all RouteWaypointLinks to RouteLinkTable
                    foreach (RouteWaypointLink r in routeWaypointsLinkList)
                        thisApp.ActiveDB.RouteLinkTable.InsertOnSubmit(r);

                    thisApp.ActiveDB.SubmitChanges();

                    MessageBox.Show("Route " + activeRoute.Name + " toegevoegd.");

                    NavigationService.GoBack();
                }
            }
            else
                MessageBox.Show("Vul een geldige naam in en voeg minimaal één Waypoint toe aan de Route.");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCreateWaypoint_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/AddNewWaypointPage.xaml", UriKind.RelativeOrAbsolute));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCreateRoute_Click(object sender, RoutedEventArgs e)
        {
            if (tbName.Text != string.Empty && tbName.Text != "Naam van de Route" )
            {
                if (NavigationHelper.CheckIfRouteExists(tbName.Text))
                    MessageBox.Show("Een Route met deze naam bestaat al. Geef deze Route een andere naam om verder te gaan.");
                else
                {
                    activeRoute = new Route();
                    activeRoute.Name = tbName.Text;

                    btnCreateRoute.IsEnabled = false;
                    btnCreateWaypoint.IsEnabled = true;
                    btnExistingWaypoint.IsEnabled = true;
                    btnMoveWaypointUp.IsEnabled = true;
                    btnMoveWaypointDown.IsEnabled = true;
                    btnRemoveWaypoint.IsEnabled = true;
                    btnAddRoute.IsEnabled = true;
                }
            }
        }
    }
}